package GDoc::Job::Fetch::US::Exec::Treas::FMS::MTS;

use Moose;


extends 'GDoc::Job::Fetch';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://fms.treas.gov/mts/backissues.html' );
    $self->_basedir( 'http://fms.treas.gov/mts/' );
}

sub run { 
    my $self = shift;
    my %data = %{ $self->data };

    if ( $data{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "Fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );
        my @links = $mech->find_all_links( url_regex => qr{mts\d{4}\.pdf} );
        
        foreach my $link( @links ) { 
            my $job = GDoc::Job->new( class => 'Fetch::US::Exec::Treas::FMS::MTS',
                                      queue => $self->fetch_queue,
                                      data  => { link => $self->_basedir . $link->url }
                                    );
            $job->submit;
        }
    }
}

sub _fetch_pdf { 
    my $self = shift;
    my $link = $self->data->{link};

    $link =~ m{mts(?<month>\d{2})(?<year>\d{2})\.pdf};
    my ( $month, $year ) = @+{'month', 'year'};
    $year = $year < 97 
      ? $year + 2000 
      : $year + 1900;    # y2k
 

    unless( $month and $year ) { 
        die "Unable to parse month and year from link $link";
    }

    my $date = sprintf( '%04d-%02d', $year, $month );
    my $ident = 'FMS-MTS-' . $date;
    my $filename = $ident . '.pdf';
    my $key = 'us/exec/treas/fms/' . $filename;

    my $mech = $self->_mech;
    $mech->get( $link );

    $self->logger->info( "Storing $link --> $key" );
    $self->store_pdf( key       => $key,
                      data      => $mech->response->content,
                      filename  => $filename,
                      meta      => { description => $date,
                                     identifier  => $ident
                                   }
                    );
}
    

1;
